<?php
namespace app\product\admin;
use app\system\admin\Admin;
use app\product\model\Project as ProjectModel;
use app\product\model\Product as ProductModel;
use app\product\model\ProjectProduct as ProjectProductModel;
// use app\product\model\Brand as BrandModel;
// use app\product\model\Supplier as SupplierModel;
use app\product\model\Unit as UnitModel;
class ProjectProduct extends Admin
{
    protected $hisiModel = 'ProjectProduct';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名
    protected function initialize()
    {
        parent::initialize();
        if ($this->request->action() != 'index' && !$this->request->isPost()) {
            $Product = ProductModel::getSelect(ProductModel::all());
            $this->assign('Product', $Product);
			//halt(ProductModel::all());
        }
    }
    public function index()
    {
        $pid = $this->request->get('pid');
        $this->assign('pid', $pid);
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $where['project_id'] = $pid;
            $unit = UnitModel::column('id,name');
            $data['data'] = ProjectProductModel::with('hasProduct')
                            ->where($where)
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['has_product']['unit'] = $unit[$data['data'][$key]['has_product']['unit_id']];
                $data['data'][$key]['has_product']['decomposition'] = $unit[$data['data'][$key]['has_product']['decomposition_id']];
            }
            $data['count'] = ProjectProductModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        // 分组切换类型 0无需分组切换，1单个分组，2多个分组切换[无链接]，3多个分组切换[有链接]，具体请看application/example/view/layout.html
        $this->assign('hisiTabType', 0);
        // tab切换数据
        // $hisiTabData = [
        //     ['title' => '后台首页', 'url' => 'system/index/index'],
        // ];
        // current 可不传
        // $this->assign('hisiTabData', ['menu' => $hisiTabData, 'current' => 'system/index/index']);
        $this->assign('hisiTabData', '');
        return $this->fetch();
    }


    public function add()
    {
        $pid = $this->request->get('pid');
        // halt($pid);
        $this->assign('pid', $pid);
        if ($this->request->isPost()) {

            $postData = $this->request->post();

            if ($this->hisiValidate) {// 数据验证

                if (strpos($this->hisiValidate, '\\') === false ) {

                    if (defined('IS_PLUGINS')) {
                        $this->hisiValidate = 'plugins\\'.$this->request->param('_p').'\\validate\\'.$this->hisiValidate;
                    } else {
                        $this->hisiValidate = 'app\\'.$this->request->module().'\\validate\\'.$this->hisiValidate;
                    }

                }

                if ($this->hisiAddScene) {
                    $this->hisiValidate = $this->hisiValidate.'.'.$this->hisiAddScene;
                }

                $result = $this->validate($postData, $this->hisiValidate);
                if ($result !== true) {
                    return $this->error($result);
                }

            }

            if ($this->hisiModel) {// 通过Model添加

                $model = $this->model();

                if (!$model->save($postData)) {
                    return $this->error($model->getError());
                }

            } else if ($this->hisiTable) {// 通过Db添加

                if (!Db::name($this->hisiTable)->insert($postData)) {
                    return $this->error('保存失败');
                }

            } else {

                return $this->error('当前控制器缺少属性（hisiModel、hisiTable至少定义一个）');

            }

            return $this->success('保存成功', '');
        }

        $template = $this->request->param('template', 'form');

        return $this->fetch($template);
    }

}
